﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Net;
using System.IO;
using System.Collections;
using System.Text.RegularExpressions;
using Newtonsoft.Json.Linq;

namespace OnlineMusic.lrc
{
    class BaiduLrc : LrcBase
    {
        private const string searchUrl = "http://sug.music.baidu.com/info/suggestion?format=json&from=0&callback=&word=";
        private const string infoUrl = "http://play.baidu.com/data/cloud/songlink?hq=0&type=m4a%2Cmp3&rate=&pt=0&flag=-1&s2p=-1&prerate=-1&bwt=-1&dur=-1&bat=-1&bp=-1&pos=-1&auto=-1&songIds=";
        private const string lrcUrl = "";
        public override string FindLrc(string title, string singer)
        {
            string result = wget(searchUrl + Uri.EscapeUriString(title + " " + singer));

            if (result.Contains("errno"))
            {
                result = wget(searchUrl + Uri.EscapeUriString(title));

                if (result.Contains("errno"))
                {
                    return null;
                }
            }

            result = result.Substring(1, result.Length - 3);
            JObject json = JObject.Parse(result);
            JArray list = (JArray)json["data"]["song"];

            if (list.Count <= 0)
            {
                return null;
            }

            JObject songItem = (JObject)list[0];

            foreach (JObject item in list)
            {
                if (title.Equals((string)item["songname"]))
                {
                    songItem = item;
                    break;
                }
            }

            string id = (string)songItem["songid"];
            string infoResult = wget(infoUrl + id);
            JObject infoJson = JObject.Parse(infoResult);
            JArray infoList = (JArray)infoJson["data"]["songList"];
            return (string)infoList[0]["lrcLink"];
        }


    }

}
